package com.web.controller.store;

import com.domain.store.Catalog;
import com.domain.store.Course;
import com.domain.system.Dept;
import com.github.pagehelper.PageInfo;
import com.service.store.CatalogService;
import com.service.store.CourseService;
import com.service.system.DeptService;
import com.utils.BeanUtil;
import com.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/catalog")
public class CatalogServlet extends HttpServlet {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    CatalogService cs= (CatalogService)ac.getBean("catalogService");
    CourseService cls= (CourseService)ac.getBean("courseService");
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String operation = request.getParameter("operation");
        if ("list".equals(operation)){
            //调用查询方法
            list(request,response);
        }else if ("toAdd".equals(operation)){
            toAdd(request,response);
        }else if ("save".equals(operation)){
            //添加方法
            save(request,response);
        }else if ("delete".equals(operation)){
            //删除方法
            delete(request,response);
        }else if ("toEdit".equals(operation)){
            toEdit(request,response);
        }else if ("edit".equals(operation)){
            //修改
            edit(request,response);
        }
    }
    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //封装数据
        Catalog Catalog = BeanUtil.fillBean(request, Catalog.class, "yyyy-MM-dd");
        //调用业务层
        cs.update(Catalog);
        //重定向
        response.sendRedirect(request.getContextPath()+"/store/catalog?operation=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获得数据id
        String id = request.getParameter("id");
        //调用业务层代码
        Catalog catalog = cs.findById(id);
        //将数据存入域中
        request.setAttribute("catalog",catalog);
        List<Course> all = cls.findAll();
        request.setAttribute("courseList",all);
        //转发
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/update.jsp").forward(request,response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //得到参数id
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class);
        //调用业务层
        cs.delete(catalog);
        //重定向
        response.sendRedirect(request.getContextPath()+"/store/catalog?operation=list");
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //使用工具类,封装bean
        Catalog catalog = BeanUtil.fillBean(request, Catalog.class, "yyyy-MM-dd");
        //调用业务层
        cs.save(catalog);
        //重定向
        response.sendRedirect(request.getContextPath()+"/store/catalog?operation=list");
    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Course> all = cls.findAll();
        request.setAttribute("courseList",all);
        //转发至添加页面
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/add.jsp").forward(request,response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //定义分页`参数
        int page = 1;
        int size = 5;
        //得到分页参数
        //判断所得参数是否为空,如果为空,使用默认得1,5
        if (StringUtils.isNoneBlank(request.getParameter("page"))){
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNoneBlank(request.getParameter("size"))){
            size = Integer.parseInt(request.getParameter("size"));
        }
        //调用业务层方法
        PageInfo all = cs.findAll(page, size);
        //将数据存至域中
        request.setAttribute("page",all);
        //转发
        request.getRequestDispatcher("/WEB-INF/pages/store/catalog/list.jsp").forward(request,response);

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
